package cn.kgc.shiro03.controller;

import cn.kgc.shiro03.entity.Permission;
import cn.kgc.shiro03.service.PermissionService;
import cn.kgc.shiro03.util.CodeMsg;
import cn.kgc.shiro03.util.ResponseResult;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/25
 *
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("list")
    public ResponseResult permissionList(){

        ResponseResult responseResult = new ResponseResult();
        List<Permission> list = permissionService.list();
        responseResult.setData(list);
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        return  responseResult;
    }

    @RequestMapping("treeSelect")
    public ResponseResult treeSelect(){
        ResponseResult  responseResult = permissionService.treeSelect();
        return  responseResult;

    }

    @RequestMapping("add")
    public ResponseResult addPermission(Permission permission){
        ResponseResult  responseResult = new ResponseResult();

        permissionService.save(permission);
        responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
        responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());

        return  responseResult;

    }

    @RequestMapping("selectById")
    public ResponseResult selectById(Integer id){
        Permission permission = permissionService.getById(id);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setData(permission);
        return responseResult;
    }

    @RequestMapping("update")
    public ResponseResult update(Permission permission){

        permissionService.updateById(permission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        return responseResult;
    }

    @RequestMapping("delete")
    public ResponseResult delete(Integer id){
        ResponseResult  responseResult =   permissionService.deleteById(id);
        return responseResult;
    }

}
